class Team {
    constructor(name) {
        this.name = name;
        this.matchesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    // 更新球队信息的方法
    updateStats(isHome, scoreHome, scoreAway) {
        if (isHome) {
            this.matchesPlayed++;
            this.goalsFor += scoreHome;
            this.goalsAgainst += scoreAway;

            if (scoreHome > scoreAway) {
                this.wins++;
                this.points += 3;
            } else if (scoreHome === scoreAway) {
                this.draws++;
                this.points += 1;
            } else {
                this.losses++;
            }
        } else {
            this.matchesPlayed++;
            this.goalsFor += scoreAway;
            this.goalsAgainst += scoreHome;

            if (scoreAway > scoreHome) {
                this.wins++;
                this.points += 3;
            } else if (scoreHome === scoreAway) {
                this.draws++;
                this.points += 1;
            } else {
                this.losses++;
            }
        }
    }
}
class League {
    constructor() {
        this.teams = [];
    }

    addTeam(team) {
        this.teams.push(team);
    }

    addMatchResult(homeTeamName, awayTeamName, homeScore, awayScore) {
        let homeTeam = this.teams.find(team => team.name === homeTeamName);
        let awayTeam = this.teams.find(team => team.name === awayTeamName);

        if (!homeTeam || !awayTeam) throw new Error('One or both teams not found.');

        homeTeam.updateStats(true, homeScore, awayScore);
        awayTeam.updateStats(false, homeScore, awayScore);
    }

    sortTeams() {
        this.teams.sort((a, b) => {
            if (b.points !== a.points) return b.points - a.points;
            if (b.goalsFor - b.goalsAgainst !== a.goalsFor - a.goalsAgainst) return (b.goalsFor - b.goalsAgainst) - (a.goalsFor - a.goalsAgainst);
            return b.goalsFor - a.goalsFor;
        });
    }

    displayStandings() {
        console.log('队伍名称\t比赛场数\t胜\t平\t负\t进球数\t失球数\t积分');
        this.teams.forEach(team => {
            console.log(`${team.name}\t${team.matchesPlayed}\t${team.wins}\t${team.draws}\t${team.losses}\t${team.goalsFor}\t${team.goalsAgainst}\t${team.points}`);
        });
    }
}


let league = new League();

// 初始化球队
let teamA = new Team('Team A');
let teamB = new Team('Team B');
let teamC = new Team('Team C');

league.addTeam(teamA);
league.addTeam(teamB);
league.addTeam(teamC);

// 添加比赛结果
league.addMatchResult('Team A', 'Team B', 2, 1); // Team A vs Team B: 2-1
league.addMatchResult('Team B', 'Team C', 1, 3); // Team B vs Team C: 1-3
league.addMatchResult('Team A', 'Team C', 0, 0); // Team A vs Team C: 0-0

// 排序并显示结果
league.sortTeams();
league.displayStandings();